interface Progress{
    key:number,
    title:string,
    total_count:number,
    right_percent:number,
    undo_percent:number,

}


// interface YearProgress{
//     year:number,
// }

interface SubjectProgress{
    title:string,
    subject_id:number,
    total_count:number,
    right_percent:number,
    undo_percent:number,
}

// interface ChapterProgress {
//     title:string,
//     chapter_id:number,
//     total_count:number,
//     right_percent:number,
//     undo_percent:number,
// }

async function get_progress_list(api:string){
    const res= await fetch(api)
    let list= (await res.json()) as Progress[]
    // list.forEach(p=>random_progress(p))
    return list
    }

    
async function get_years_progress(){
    const api='http://127.0.0.1:8000/progress/years'
    return await get_progress_list(api)
    }

async function get_subject_progress(sid:number) {
    const api='http://127.0.0.1:8000/progress/subject/'+sid
    const res= await fetch(api)
    let progress= (await res.json()) as SubjectProgress
    // random_progress(progress)
    return progress
}

async function get_chapters_progress(sid:number){
    const api='http://127.0.0.1:8000/progress/chapters?subject_id='+sid
    return await get_progress_list(api)
}

function random_progress(progress:Progress){
    progress.right_percent=Math.random()
    progress.undo_percent=Math.min(Math.random(),1-progress.right_percent)
}

export {Progress,get_years_progress,get_subject_progress,get_chapters_progress}